#include "AdminFoodItem.h"
#include "ui_AdminFoodItem.h"
#include <QDebug>
#include "FoodTable.h"

AdminFoodItem::AdminFoodItem(QListWidget *parent) :
    QListWidget(parent),
    ui(new Ui::AdminFoodItem)
{
    ui->setupUi(this);
}

AdminFoodItem::~AdminFoodItem()
{
    delete ui;
}

//初始化ui界面
void AdminFoodItem::init(Food food){
    ui->name_label->setText(food.name); //名字
    ui->price_label->setText(QString::number(food.price));  //价格
    if(food.is_null){
        ui->control_btn->setText("上架");
    }else{
        ui->control_btn->setText("下架");
    }
}


//点击下架按钮
void AdminFoodItem::on_control_btn_clicked(){
    QString name = ui->name_label->text();
    FoodTable food_table;

    if(ui->control_btn->text() == "上架"){
        //先修改ui,下架->上架,上架->下架
        ui->control_btn->setText("下架");
        /**
          * @leiteorz
          * 点击上架后,把is_null修改成0
          */
       food_table.updateFood(name,0);
    }else{
        //先修改ui,下架->上架,上架->下架
        ui->control_btn->setText("上架");
        /**
          * @leiteorz
          * 点击下架后,把is_null修改成1
          */
       food_table.updateFood(name,1);
    }

    emit dataChange();
}

//点击查看按钮
void AdminFoodItem::on_query_btn_clicked(){
    FoodTable food_table;
    QString name = ui->name_label->text();
    Food food;

    food = food_table.queryFoodByName(name);

    EditFoodWindow *edit_food_window = new EditFoodWindow();
    edit_food_window->init(food);
    edit_food_window->show();

    connect(edit_food_window,SIGNAL(dataChange()),this,SLOT(replyDataChange()));
}

void AdminFoodItem::replyDataChange(){
    emit dataChange2();
}
